package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.entity.Department;
import com.lin.mapper.DepartmentMapper;
import com.lin.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lin
 * @since 2022-06-17
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public Page<Department> getDepartmentList(Integer page, Integer size, Integer condition, String key) {
        Page<Department> page1 = new Page<>(page,size);

        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(condition == 1, Department::getName, key)
                .like(condition == 2, Department::getId, key)
                .like(condition == 3, Department::getChargeName, key)
                .like(condition == 4, Department::getPhone, key);
        Page<Department> page2 = page(page1, queryWrapper);
        return page2;
    }


    @Override
    public Boolean addDepartment(Department department) {
        //检查字段完整性
        if (department.getId() == null || department.getName() == null || department.getName().isEmpty() || department.getId().isEmpty())
            return false;//这里应该抛出异常，这里先false处理
        return this.save(department);
    }

    @Override
    public Boolean updateDepartment(Department department) {
        return this.updateById(department);
    }

    @Override
    public Boolean removeDepartment(Integer did) {
        return this.removeById(did);
    }
}
